package com.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 阿里云 - 对象存储OSS工具
 */
public class OSSUtil {

	private volatile static OSSClient instance;

	private static final Logger log = LoggerFactory.getLogger(OSSUtil.class);

    private OSSUtil() {
	    }
	 
	    /**
	     * 单例
	     * @return  OSS工具类实例
	     */
	    private static OSSClient getOSSClient() {
	        if (instance == null) {
	            synchronized (OSSUtil.class) {
	                if (instance == null) {
						//log.warn("阿里云 - 对象存储OSS - 认证信息: endPoint:{} and accessKeyId:{} and accessKeySecret:{}", AliyunConfig.getEndPoint(), AliyunConfig.getAccessKeyId(), AliyunConfig.getAccessKeySecret());
	                    instance = new OSSClient(AliyunConfig.getEndPoint(), AliyunConfig.getAccessKeyId(), AliyunConfig.getAccessKeySecret());
	                }
	            }
	        }
	        return instance;
	    }

	    //设置URL过期时间为100年
	    private final static Date OSS_URL_EXPIRATION = DateUtils.addDays(new Date(), 365 * 100);
	 
	    //文件路径的枚举
	    public enum FileDirType {
	        RUIJU("ruiju");
	        private final String dir;
	        FileDirType(String dir) {
	            this.dir = dir;
	        }
	 
	        @JsonValue
	        public String getDir() {
	            return dir;
	        }
	    }
	 
	    /**
	     * 上传文件---去除URL中的？后的时间戳
	     * @param file 文件
	     * @param fileDir 上传到OSS上文件的路径
	     * @return 文件的访问地址
	     * @throws Exception 
	     */
	    public static String upload(MultipartFile file, FileDirType fileDir) {
	        OSSUtil.createBucket();
	        String fileName = OSSUtil.uploadFile(file, fileDir);
	        String fileOssURL = OSSUtil.getImgUrl(fileName, fileDir);
	        int firstChar = fileOssURL.indexOf("?");
	        if (firstChar > 0) {
	            fileOssURL = fileOssURL.substring(0, firstChar);
	        }
	        return fileOssURL;
	    }

	/**
	 * 上传来源网络的图片
	 * @param sourceImgUrl
	 * @return
	 * @throws Exception
	 */
	public static String uploadFile(String sourceImgUrl, String newImgName) throws Exception {

		String fileOssURL = "";
		InputStream inputStream = null;
		try {
			if (!StringUtils.isEmpty(sourceImgUrl)) {
				//Europe
				int europeFlag = sourceImgUrl.indexOf("https://1431182383.rsc.cdn77.org");
				if (-1 !=europeFlag&&-1 !=sourceImgUrl.indexOf(" ")) {
					sourceImgUrl = sourceImgUrl.replaceAll(" ","%20");
				}
				URL url = new URL(sourceImgUrl);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				//设置超时间为10秒
				conn.setConnectTimeout(10 * 1000);
				//防止屏蔽程序抓取而返回403错误
				conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

				//图片尺寸处理，用于以图搜图
				//inputStream = imageDizeDeal(inputStream);

                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK&&StringUtils.isNotEmpty(newImgName)) {
					//得到输入流
					inputStream = conn.getInputStream();
					fileOssURL = OSSUtil.aliYunUploadFile(inputStream, newImgName);
				}
			}

		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传图片 - 失败", e);
		} finally {
			if (null != inputStream) {
				inputStream.close();
			}
		}

		//log.warn("阿里云 - 对象存储OSS - 上传图片 - 完成, fileOssURL:{} ", fileOssURL);
		return fileOssURL;
	}

	/**
	 * 获取上传图片新名称
	 * @param sourceImgUrl
	 * @param barCode
	 * @return
	 * @throws Exception
	 */
	public static String uploadFileName(String sourceImgUrl, String barCode) throws Exception{

		String newImgName = "";
		String suffixName = "";

		//Buyer
		int buyerFlag = sourceImgUrl.indexOf("http://buyer-app-img.cmsok.com");
		if (-1 != buyerFlag) {
			int buyerEndChar = sourceImgUrl.lastIndexOf("?");
			int firstChar = sourceImgUrl.lastIndexOf(".");
			suffixName = sourceImgUrl.substring(firstChar, buyerEndChar);
			newImgName = "search-buyer-" + barCode + suffixName;
		}

		//Europe
		int europeFlag = sourceImgUrl.indexOf("https://1431182383.rsc.cdn77.org");
		if (-1 !=europeFlag) {
			int europeEndChar = sourceImgUrl.lastIndexOf(".");
			suffixName = sourceImgUrl.substring(europeEndChar);
			newImgName = "search-europe-" + barCode + suffixName;
		}

		return newImgName;
	}

	/**
	 * 图片尺寸处理
	 */
	private static InputStream imageDizeDeal(InputStream inputStream) throws IOException {

		//图片尺寸调整，用于以图搜图
		int imgMinWidth = 128;
		int imgMinHeight = 128;

		int imgMaxWidth = 1024;
		int imgMaxHeight = 1024;

		//字节流转图片对象
		BufferedImage bufImg = ImageIO.read(inputStream);
		if (!(bufImg.getWidth()>imgMinWidth&&bufImg.getHeight()>imgMinHeight&&
				bufImg.getWidth()<imgMaxWidth&&bufImg.getHeight()<imgMaxHeight)) {
			// 存储图片文件byte数组
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			//防止图片变红
			BufferedImage newBufferedImage = new BufferedImage(imgMaxWidth, imgMaxHeight, BufferedImage.TYPE_INT_RGB);
			newBufferedImage.createGraphics().drawImage(bufImg, 0, 0, Color.WHITE, null);
			//先转成jpg格式来压缩,然后在通过OSS来修改成源文件本来的后缀格式
			ImageIO.write(newBufferedImage, "jpg", bos);
			//获取输出流
			inputStream = new ByteArrayInputStream(bos.toByteArray());
		}
		return inputStream;
	}


	/**
         * 当Bucket不存在时创建Bucket
         *
         * @throws OSSException 异常
         * Bucket命名规则：
         *  1.只能包含小写字母、数字和短横线，
         *  2.必须以小写字母和数字开头和结尾
         *  3.长度在3-63之间
         */
	    private static void createBucket() {
	        try {
	            if (!OSSUtil.getOSSClient().doesBucketExist(AliyunConfig.getBucketName())) {//判断是否存在该Bucket，不存在时再重新创建
	                OSSUtil.getOSSClient().createBucket(AliyunConfig.getBucketName());
	            }
	        } catch (Exception e) {
	          //  logger.error("{}", "创建Bucket失败,请核对Bucket名称(规则：只能包含小写字母、数字和短横线，必须以小写字母和数字开头和结尾，长度在3-63之间)");
	          //  throw new OSSCreateBucketRuntimeException("创建Bucket失败,请核对Bucket名称(规则：只能包含小写字母、数字和短横线，必须以小写字母和数字开头和结尾，长度在3-63之间)");
	        	System.out.println("创建Bucket失败,请核对Bucket名称(规则：只能包含小写字母、数字和短横线，必须以小写字母和数字开头和结尾，长度在3-63之间)");
	        }
	    }
	 
	 
	    /**
	     * 上传到OSS服务器  如果同名文件会覆盖服务器上的
	     * @param file 文件
	     * @param fileDir  上传到OSS上文件的路径
	     * @return 文件的访问地址
	     * @throws Exception 
	     */
	    private static String uploadFile(MultipartFile file, FileDirType fileDir) {
	        String fileName = String.format(
	                "%s.%s",
	                UUID.randomUUID().toString(),
	                FilenameUtils.getExtension(file.getOriginalFilename()));

	        try (InputStream inputStream = file.getInputStream()) {
	            //创建上传Object的Metadata
	            ObjectMetadata objectMetadata = new ObjectMetadata();
	            objectMetadata.setContentLength(inputStream.available());
	            objectMetadata.setCacheControl("no-cache");
	            objectMetadata.setHeader("Pragma", "no-cache");
	            objectMetadata.setContentType(getContentType(FilenameUtils.getExtension("." + file.getOriginalFilename())));
	            objectMetadata.setContentDisposition("inline;filename=" + fileName);
	            //上传文件
				String oldName = file.getOriginalFilename();
				//获取后缀名
				String imgType = oldName.substring(oldName.lastIndexOf("."));
				//拼接新的文件名
				String newName = fileName + imgType;
	            PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), fileDir.getDir() + newName, inputStream, objectMetadata);
	            return fileName;
	        } catch (Exception e) {
	            //logger.error("{}", "上传文件失败");
	         //   throw new OssPutObjectRuntimeException("上传文件失败");
	        	e.printStackTrace();
	        }
	        return null;
	    }

	/**
	 * AliYun OSS 上传接口
	 *
	 * @param file
	 * @param fileName
	 * @return
	 */
	public static String aliYunUploadFile(InputStream file, String fileName) {

		if (StringUtils.isEmpty(fileName)){
			return fileName;
		}

		OSSClient ossClient = OSSUtil.getOSSClient();
		try {
			//容器不存在，就创建
			if (!ossClient.doesBucketExist(AliyunConfig.getBucketName())) {
				ossClient.createBucket(AliyunConfig.getBucketName());
				CreateBucketRequest createBucketRequest = new CreateBucketRequest(AliyunConfig.getBucketName());
				createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
				ossClient.createBucket(createBucketRequest);
			}
			//创建文件路径
			String key = AliyunConfig.getFilePath() + fileName;
			//log.warn("阿里云 - 对象存储OSS - 上传图片 - 传入参数: bucketName:{} and key:{}", AliyunConfig.getBucketName(), key);
			//上传文件
			PutObjectResult result = ossClient.putObject(new PutObjectRequest(AliyunConfig.getBucketName(), key, file));
			//设置权限 这里是公开读
			ossClient.setBucketAcl(AliyunConfig.getBucketName(), CannedAccessControlList.PublicRead);

			if (null != result) {
				return AliyunConfig.getOssUrl() + key;
			}
		} catch (OSSException e) {
			log.error("阿里云 - 对象存储OSS - 上传图片 - 失败", e);
		}
		return "";
	}

	public static String uploadLocalExcel(String filePath, MultipartFile file, String fileName) throws Exception {
		String imageUrl = null;
		//获取图片类型
		String excelType = getExcelType(FilenameUtils.getExtension("." + file.getOriginalFilename()));
		if (StringUtils.isEmpty(excelType)){
			throw new Exception("文件格式不合法，必须为csv,xls,xlsx");
		}
		try {
			InputStream inputStream = file.getInputStream();
			//创建上传Object的Metadata
			ObjectMetadata objectMetadata = new ObjectMetadata();
			objectMetadata.setContentLength(inputStream.available());
			objectMetadata.setCacheControl("no-cache");
			objectMetadata.setHeader("Pragma", "no-cache");
			objectMetadata.setContentType(excelType);
			objectMetadata.setContentDisposition("inline;filename=" + fileName);
			//创建文件路径
			String key = filePath + fileName;
			PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), key, inputStream, objectMetadata);
			if (null != putResult) {
				imageUrl = AliyunConfig.getOssUrl() + key;
			}
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传Excel - 失败:"+ e.getMessage(), e);
		}

		return imageUrl;
	}

	/**
	 * 本地图片文件上传到OSS服务器
	 * 如果同名文件会覆盖服务器上的
	 * @param file 文件
	 * @return 文件的访问地址
	 * @throws Exception
	 */
	public static String uploadLocalImage(String filePath, MultipartFile file, String fullName) throws Exception {

		String imageUrl = null;

		//获取图片类型
		String imageType = getImageType(FilenameUtils.getExtension("." + file.getOriginalFilename()));

		if (StringUtils.isEmpty(imageType)){
			throw new Exception("图片格式不合法，必须为JPG、JPEG、PNG");
		}

		try {
			InputStream inputStream = file.getInputStream();

			//图片尺寸处理, 有点问题 上传图片失效
			//inputStream = imageDizeDeal(inputStream);

			//创建上传Object的Metadata
			ObjectMetadata objectMetadata = new ObjectMetadata();
			objectMetadata.setContentLength(inputStream.available());
			objectMetadata.setCacheControl("no-cache");
			objectMetadata.setHeader("Pragma", "no-cache");
			objectMetadata.setContentType(imageType);
			objectMetadata.setContentDisposition("inline;filename=" + fullName);
			//创建文件路径
			//判断filePath是否为空
			//空：过滤filePath 非空：添加filePath
			String key = StringUtils.isBlank(filePath) ? fullName : filePath + fullName;
			PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), key, inputStream, objectMetadata);
			if (null != putResult) {
				imageUrl = AliyunConfig.getOssUrl() + key;
			}

		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传图片 - 失败:"+ e.getMessage(), e);
		}

		return imageUrl;
	}
	
	/**
	 * 本地图片文件上传到OSS服务器
	 * 如果同名文件会覆盖服务器上的
	 * @param file 文件
	 * @return 文件的访问地址
	 * @throws Exception
	 */
	public static String uploadLocalVideo(String filePath, MultipartFile file, String fullName) throws Exception {
		
		String imageUrl = null;
		
		//获取图片类型
		String videoType = getVideoType(FilenameUtils.getExtension("." + file.getOriginalFilename()));
		
		if (StringUtils.isEmpty(videoType)){
			throw new Exception("视频格式不合法，必须为mp4,3gp,m3u8");
		}
		
		try {
			InputStream inputStream = file.getInputStream();
			
			//图片尺寸处理, 有点问题 上传图片失效
			//inputStream = imageDizeDeal(inputStream);
			
			//创建上传Object的Metadata
			ObjectMetadata objectMetadata = new ObjectMetadata();
			objectMetadata.setContentLength(inputStream.available());
			objectMetadata.setCacheControl("no-cache");
			objectMetadata.setHeader("Pragma", "no-cache");
			objectMetadata.setContentType(videoType);
			objectMetadata.setContentDisposition("inline;filename=" + fullName);
			//创建文件路径
			//判断filePath是否为空
			//空：过滤filePath 非空：添加filePath
			String key = StringUtils.isBlank(filePath) ? fullName : filePath + fullName;
			PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), key, inputStream, objectMetadata);
			if (null != putResult) {
				imageUrl = AliyunConfig.getOssUrl() + key;
			}
			
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传视频 - 失败:"+ e.getMessage(), e);
		}
		
		return imageUrl;
	}
	
	/**
	 * 本地图片文件上传到OSS服务器
	 * 如果同名文件会覆盖服务器上的
	 * @param file 文件
	 * @return 文件的访问地址
	 * @throws Exception
	 */
	public static String uploadLocalVideo(String filePath, File file, String fullName) throws Exception {
		
		String imageUrl = null;
		
		//获取图片类型
		String videoType = getVideoType(FilenameUtils.getExtension(fullName));
		
		if (StringUtils.isEmpty(videoType)){
			throw new Exception("视频格式不合法，必须为mp4,3gp,m3u8");
		}
		
		try {
			InputStream inputStream = new FileInputStream(file);
			
			//图片尺寸处理, 有点问题 上传图片失效
			//inputStream = imageDizeDeal(inputStream);
			
			//创建上传Object的Metadata
			ObjectMetadata objectMetadata = new ObjectMetadata();
			objectMetadata.setContentLength(inputStream.available());
			objectMetadata.setCacheControl("no-cache");
			objectMetadata.setHeader("Pragma", "no-cache");
			objectMetadata.setContentType(videoType);
			objectMetadata.setContentDisposition("inline;filename=" + fullName);
			//创建文件路径
			//判断filePath是否为空
			//空：过滤filePath 非空：添加filePath
			String key = StringUtils.isBlank(filePath) ? fullName : filePath + fullName;
			PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), key, inputStream, objectMetadata);
			if (null != putResult) {
				imageUrl = AliyunConfig.getOssUrl() + key;
			}
			
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传视频 - 失败:"+ e.getMessage(), e);
		}
		
		return imageUrl;
	}

	public static String uploadLocalImage(String filePath, File file, String fileName) throws Exception {
		String imageUrl = null;
		//获取图片类型
		String imageType = fileName.substring(fileName.lastIndexOf("."));

		if (StringUtils.isEmpty(imageType)){
			throw new Exception("图片格式不合法，必须为JPG、JPEG、PNG");
		}
		try {
			//创建文件路径
			//判断filePath是否为空
			//空：过滤filePath 非空：添加filePath
			String key = StringUtils.isBlank(filePath) ? fileName : filePath + fileName;
			PutObjectResult putResult = OSSUtil.getOSSClient().putObject(AliyunConfig.getBucketName(), key, file);
			if (null != putResult) {
				imageUrl = AliyunConfig.getOssUrl() + key;
			}
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 上传图片 - 失败:"+ e.getMessage(), e);
		}
		return imageUrl;
	}


	public static String getLocalFileName(MultipartFile file, String fileName) throws Exception{

		//文件格式后缀
		String fileSuffix = FilenameUtils.getExtension(file.getOriginalFilename());

		//拼接文件全名称
		return String.format("%s.%s",fileName ,fileSuffix);
	}

	private static String getImgUrl(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			throw new RuntimeException("文件地址为空");
		}
		//获取oss图片URL失败
		URL url = OSSUtil.getOSSClient().generatePresignedUrl(AliyunConfig.getBucketName(), fileUrl, OSS_URL_EXPIRATION);
		if (url == null) {
			log.error("商品图片上传到阿里云OSS服务器 - 获取oss文件URL失败");
		}
		return url.toString();
	}
	    /**
	     * 获得文件路径
	     * @param fileUrl  文件的URL
	     * @param fileDir  文件在OSS上的路径
	     * @return 文件的路径
	     */
	    private static String getImgUrl(String fileUrl, FileDirType fileDir) {
	        if (StringUtils.isEmpty(fileUrl)) {
	         //   logger.error("{}", "文件地址为空");
	            throw new RuntimeException("文件地址为空");
	        }
	        String[] split = fileUrl.split("/");
	 
	        //获取oss图片URL失败
	        URL url = OSSUtil.getOSSClient().generatePresignedUrl(AliyunConfig.getBucketName(), fileDir.getDir() + split[split.length - 1], OSS_URL_EXPIRATION);
	        if (url == null) {
	       //     logger.error("{}", "获取oss文件URL失败");
	        //    throw new OSSGeneratePresignedUrlRuntimeException("获取oss文件URL失败");
	        	System.err.println("获取oss文件URL失败");
	        }
	        return url.toString();
	    }

	/**
	 * 判断OSS服务文件上传时图片
	 * 图片格式：JPG、JPEG、PNG
	 *
	 * @param FilenameExtension 文件后缀
	 * @return 后缀
	 */
	private static String getImageType(String FilenameExtension) {
		if (FilenameExtension.equalsIgnoreCase("jpeg") ||
				FilenameExtension.equalsIgnoreCase("jpg") ||
				FilenameExtension.equalsIgnoreCase("png") ||
				FilenameExtension.equalsIgnoreCase("bmp")) {
			return "image/jpg";
		}

		return "";
	}
	
	public static String getVideoType(String FilenameExtension) {
		if (FilenameExtension.equalsIgnoreCase("mp4") ||
				FilenameExtension.equalsIgnoreCase("3gp") ||
				FilenameExtension.equalsIgnoreCase("m3u8")) {
			return FilenameExtension;
		}
		
		return "";
	}

	public static String getExcelType(String FilenameExtension) {
		if (FilenameExtension.equalsIgnoreCase("csv") ||
				FilenameExtension.equalsIgnoreCase("xls") ||
				FilenameExtension.equalsIgnoreCase("xlsx")) {
			return FilenameExtension;
		}

		return "";
	}

	    /**
	     * 判断OSS服务文件上传时文件的contentType
	     *
	     * @param FilenameExtension 文件后缀
	     * @return 后缀
	     */
	    private static String getContentType(String FilenameExtension) {
	        if (FilenameExtension.equalsIgnoreCase("bmp")) {
	            return "image/bmp";
	        }
	        if (FilenameExtension.equalsIgnoreCase("gif")) {
	            return "image/gif";
	        }
	        if (FilenameExtension.equalsIgnoreCase("jpeg") ||
	                FilenameExtension.equalsIgnoreCase("jpg") ||
	                FilenameExtension.equalsIgnoreCase("png")) {
	            return "image/jpeg";
	        }
	        if (FilenameExtension.equalsIgnoreCase("html")) {
	            return "text/html";
	        }
	        if (FilenameExtension.equalsIgnoreCase("txt")) {
	            return "text/plain";
	        }
	        if (FilenameExtension.equalsIgnoreCase("vsd")) {
	            return "application/vnd.visio";
	        }
	        if (FilenameExtension.equalsIgnoreCase("pptx") ||
	                FilenameExtension.equalsIgnoreCase("ppt")) {
	            return "application/vnd.ms-powerpoint";
	        }
	        if (FilenameExtension.equalsIgnoreCase("docx") ||
	                FilenameExtension.equalsIgnoreCase("doc")) {
	            return "application/msword";
	        }
	        if (FilenameExtension.equalsIgnoreCase("xml")) {
	            return "text/xml";
	        }
	        
	        /**
	         * 下面是对视频文件的处理
	         */
	        if (FilenameExtension.equalsIgnoreCase("mp4")) {
	            return "video/mpeg4";
	        }
	        
	        if (FilenameExtension.equalsIgnoreCase("rmvb")) {
	            return "application/vnd.rn-realmedia-vbr";
	        }
	        
	        if (FilenameExtension.equalsIgnoreCase("wmv")) {
	            return "video/x-ms-wmv";
	        }
	        
	        if (FilenameExtension.equalsIgnoreCase("avi")) {
	            return "video/avi";
	        }
	        
	        if (FilenameExtension.equalsIgnoreCase("avi")) {
	            return "video/avi";
	        }
	        
	        if (FilenameExtension.equalsIgnoreCase("wav")) {
	            return "audio/wav";
	        }
	        
	        return "image/jpeg";
	    }

	/**
	 * 删除OSS图片
	 *
	 * 如需删除文件夹，请将ObjectName设置为对应的文件夹名称。
	 * 如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
	 * @return
	 * @throws Exception
	 */
	public static void deleteFile(String filePath, String fileName) throws Exception {
		try{
			OSSClient ossClient = OSSUtil.getOSSClient();
			String key = filePath + fileName;
			ossClient.deleteObject(AliyunConfig.getBucketName(), key);
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 删除图片 - 失败", e);
		}
		//log.warn("阿里云 - 对象存储OSS - 删除图片 - 成功, fileName:{} ", fileName);
	}

	public static void deleteLocalFile( String filePath, String fileName) throws Exception {
		try{
			OSSClient ossClient = OSSUtil.getOSSClient();
			String key = filePath + fileName;
			ossClient.deleteObject(AliyunConfig.getBucketName(), key);
		} catch (Exception e) {
			log.error("阿里云 - 对象存储OSS - 删除图片 - 失败", e);
		}
		log.warn("阿里云 - 对象存储OSS - 删除图片 - 成功, fileName:{} ", fileName);
	}

	/**
	 * 判断OSS图片是否存在
	 *
	 * @return
	 * @throws Exception
	 */
	public static boolean selectFile(String fileName) throws Exception {
		OSSClient ossClient = OSSUtil.getOSSClient();
		if (StringUtils.isNotEmpty(fileName)) {
			OSSObject obj = ossClient.getObject(AliyunConfig.getBucketName(), fileName);
			return null != obj;
		}
		return false;
	}
}
